package com.cooldatasoft.menu;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.IPageLink;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import com.cooldatasoft.app.ProductApplication;
import com.cooldatasoft.data.Node;
import com.cooldatasoft.data.Person;
import com.cooldatasoft.web.product.main.OrganizationChart;
import com.cooldatasoft.web.product.person.PersonPage;

public class NodeBox extends Panel{

	public NodeBox(String id, final Integer nodeId,Boolean showSubPersonList) {
		super(id);
		final AttributeModifier attributeModifier = new AttributeModifier("target",true,new Model("_blank"));
		
		Node rootNode = ProductApplication.get().getNodeServiceProxy().get(nodeId);	
		
//		nodeManagerTitle
		
		//Title of the box
		PageLink departmentNameLink = new PageLink("departmentNameLink", new IPageLink() {
			public Page getPage() {
				return new OrganizationChart(nodeId);
			}
			public Class getPageIdentity() {
				return OrganizationChart.class;
			}
		});
		Label titleLabel = new Label("label",rootNode.getLabel());
		titleLabel.setRenderBodyOnly(true);
		departmentNameLink.add(titleLabel);
		
		
		//Manager of the Box
		final Person manager = ProductApplication.get().getPersonServiceProxy().get(rootNode.getManagerId());
		String name = ProductApplication.get().getPersonServiceProxy().getName(manager.getId(),false);
		String surname = ProductApplication.get().getPersonServiceProxy().getSurname(manager.getId(),false);
		String position = ProductApplication.get().getPersonServiceProxy().getPosition(manager.getId(),false);
		
		PageLink nodeManagerLink = new PageLink("nodeManagerLink", new IPageLink() {
			public Page getPage() {
				return new PersonPage(manager.getId());
			}
			public Class getPageIdentity() {
				return PersonPage.class;
			}
		});
		
		Label managerTitle = new Label("nodeManagerTitle",position);
		Label managerNameSurnameLabel = new Label("label",name+" "+surname);
		managerNameSurnameLabel.setRenderBodyOnly(true);
		nodeManagerLink.add(managerNameSurnameLabel);		
		nodeManagerLink.add(attributeModifier);
		
		List<Person> personList = new ArrayList<Person>();
		//Dont show any sub person list for the top root node
		if(showSubPersonList){
			//Retrieve person object connected to this node
			personList = ProductApplication.get().getPersonServiceProxy().getPersonList(rootNode.getManagerId());
			//Find Managers of child nodes
			List<Node> childNodeList = ProductApplication.get().getNodeServiceProxy().getChildrenNodeList(rootNode.getId());
			//Add Child Node managers to person list
			for(Node node:childNodeList){
				Integer childNodeManagerId = node.getManagerId();
				Person childNodeManager = ProductApplication.get().getPersonServiceProxy().get(childNodeManagerId);
				if(!personList.contains(childNodeManager)){
					personList.add(childNodeManager);
				}
			}
			//Remove manager from the list
			for(Person person:personList){
				if(person.getId().equals(manager.getId())){
					personList.remove(person);
					break;
				}
			}
		}	
		
		ListView groupMembers = new ListView("groupMembers", personList) {
		    protected void populateItem(ListItem item) {
		       final Person currentPerson = (Person) item.getModelObject();
		       String name = ProductApplication.get().getPersonServiceProxy().getName(currentPerson.getId(),false);
		       String surname = ProductApplication.get().getPersonServiceProxy().getSurname(currentPerson.getId(),false);
				
		       PageLink memberLink = new PageLink("memberLink", new IPageLink() {
		    	   public Page getPage() {
		    		   return new PersonPage(currentPerson.getId());
		    	   }
		    	   public Class getPageIdentity() {
		    		   return PersonPage.class;
		    	   }
		       });
		       Label memberNameSurnameLabel = new Label("label",name+" "+surname);
		       memberNameSurnameLabel.setRenderBodyOnly(true);
		       memberLink.add(memberNameSurnameLabel);
		       memberLink.add(attributeModifier);
		       item.add(memberLink);
		    }
		};
		groupMembers.setOutputMarkupPlaceholderTag(false);
		
		//Dont render line if there is no person to list in the subperson list.
		//Dont show any sub person list for the top root node
		WebMarkupContainer line = new WebMarkupContainer("line");
		if(personList.size()<1 || !showSubPersonList){
			line.setRenderBodyOnly(true);
		}
		
		add(line);
		add(departmentNameLink);
		add(nodeManagerLink);
		add(managerTitle);
		add(groupMembers);		
	}

}
